package com.service.db.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.service.bean.device.Vehicle;
import com.service.db.DbConnectionManager;
import com.service.db.PooledConnection;
import com.service.other.Logger;

public class DBSpeed {
	/**
	 * 1H更新一次副发时长
	 * @param oilCard
	 * @param oilleftamt
	 * @param company
	 */
	public static long getSubSpeedTime(Vehicle vehicle) {
		PooledConnection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select subSpeedTime from crc_Equipment where EquipmentId=?";
		long subSpeedTime = 0 ;
		try {
			con = DbConnectionManager.getConnection(vehicle.company);
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, vehicle.equipmentId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				subSpeedTime = rs.getLong("subSpeedTime");
			}
		} catch (Exception e) {
			Logger.err(e);
		} finally {
			try {
				if (null != rs)
					rs.close();
				if (null != pstmt)
					pstmt.close();
				if (null != con)
					con.close();
			} catch (Exception ex) {
				Logger.err(ex);
			}
		}
		return subSpeedTime;
	}
	
	/**
	 * 1H更新一次副发时长
	 * @param oilCard
	 * @param oilleftamt
	 * @param company
	 */
	public static void updateSubSpeedTime(Vehicle vehicle,long time) {
		PooledConnection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DbConnectionManager.getConnection(vehicle.company);
			pstmt = con.prepareStatement("update crc_Equipment set subSpeedTime=? where EquipmentId=?");
			pstmt.setDouble(1, time);
			pstmt.setString(2, vehicle.equipmentId);
			pstmt.execute();
		} catch (Exception e) {
			Logger.err(e);
		}finally{
			try{
				if(pstmt!=null && !pstmt.isClosed()){
					pstmt.close();
				}
				if (con != null) {
					con.close();
				}
			}catch(Exception e){
				Logger.err(e);
			}
		}
	}

}
